package request;

import core.*;
import java.io.DataInputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.util.Date;

/**
 * Request object.  Create version of this
 * class for each request type.
 *
 * @author eric, yiyi, Gary
 */
public abstract class GameRequest {

    // Reference to the game client which handles the request
    protected GameClient client;
    // Reference to game server.
    protected GameServer gameServer;
    // Input data stream from socket
    protected DataInputStream dataIn;
    // Time when this request comes.
    protected Date timestamp;
    // The total size of the package to be read
    protected int size;
    // Response Code
    protected int code;

    public void set(int size, DataInputStream dataIn, GameClient client, GameServer server) {
        this.size = size;
        System.out.println("the game req size : "+size);
        System.out.println("the game req data in: "+dataIn.toString());
        this.dataIn = dataIn;
        this.client = client;
        this.gameServer = server;

        timestamp = new Date(); // Defaults to current time.
    }

    /**
     * Parses the request.
     */
    public abstract void parse();

    /**
     * Generate response
     * @param output	the outputstream to the socket
     */
    public abstract void respond(OutputStream output);

    /**
     * Get the package size
     * @return		package size
     */
    public int getPackageLength() {
        return size;
    }

    /**
     * Get the timestamp of this request
     * @return		the timestamp
     */
    public Date getTimestamp() {
        return timestamp;
    }

    /**
     * Returns the corresponding response code
     *
     * @return code
     */
    public int getCode() {
        return code;
    }
}
